#!/bin/bash
errors=''
#MAKE SURE MYSQL IS INSTALLED AND THAT THE REQUIRED DATABASE IS PRESENT
if !( which mysql ); then
	echo "No mysql installed here. Would you like me to install it? (Y or N)"
	read response
		if [ $response == "Y" ]; then
			bash sys_resources/mysql.install
			wait
		else
			echo "Nothing left to do;"
		exit
		fi	
else
	aminstalled=`mysql -u root --password=carblogger -e "DESCRIBE cl_ingests;" carblogger | wc -l`
	if [ $aminstalled -lt 3 ]; then
		echo "Hmm...lights are on but no-one is home. Want me to set up a carblogger database for you?"
		read response
		if [ $response=="Y" ]; then
			echo "Installing the ingest tables"
			mysql -u root --password=carblogger < sys_resources/alldb_backup.sql				
		fi
	fi
fi

#HACK to grab arrays from translate_code.sh
#dms array is dmscode
#raw text array is rawcode
source /home/aqdms/CARBLog/sys_resources/translate_code.sh displaycode 1 rawcode

parsedata () {
	while read line; do
		is_disabled=0		
		this_entry="$line"
		#dash=`echo $this_entry | grep -m 1 ' [.]*[A-Z]*--' | wc -l`
		dash="${this_entry: -2}"
		#this_entry=`echo $line | sed 's/:::/ /g' | sed 's/::/ /g'`
		#MODIFIED BY MJV TO USE THE TRANSLATE CODE TO CHANGE OFFLINE ENTRY
		#BASED UPONE THE TYPE OF OFFLINE CHOSEN BY THE OPERATOR	
		#if [ $dash -ne "0" ]; then		
		if [ "$dash" == "--" ]; then
			COUNTER=1
			while [[ $COUNTER -lt 7 ]];	do
				#rawpat=`echo "${rawcode[$COUNTER]}" | sed 's/--/\\\\-\\\\-/g'`
			    rawpat=${rawcode[$COUNTER]/#--/\\-\\-}
		        rawpat=${rawpat/%--/\\-\\-}								
				#echo "$rawpat"
				#dmsout="${dmscode[$COUNTER]}"
				#echo "My dmscode is $dmsout"
		
				if [ `echo $this_entry | grep -o "$rawpat"` > 0 ]; then
					#this_entry=`echo $this_entry | sed 's/'$rawout'//g'`
					is_disabled="${dmscode[$COUNTER]}"
					COUNTER=7
					#echo "Found $rawout in $this_entry using $rawpat set dmscode to $dmsout"
				fi 
				((COUNTER++))
			done
		fi
		#EXTRACT THE DATE STAMP FROM EACH LINE 
		tempdate=`echo $this_entry | grep -o [0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]_[0-9][0-9]:[0-9][0-9] | sed 's/$/:00/g'`
		thisdate="${tempdate:0:10}"
		#EXTRACT THE TIME STAMP FROM EACH LINE
		thistime="${tempdate:11:8}"
		#EXTRACT THE NUMERICAL VALUE TO BE STORED IN THE DATABASE
		#USING THE RECOGNITION OF THE [PATTERN=][VALUE] THEN REMOVE THE PATTERN
		thisvalue=`echo $this_entry | grep -o ${i}[-.0-9]* | sed 's/'$i'//g'` 
		#IF THE DATA HAS BEEN PRODUCED WHILE/AFTER DISCONNECTED THEN SET VALUE TO ND
		if [[ -z $thisvalue ]]; then
			#INDICATE NO VALUE
		 	thisvalue="NV"
		else
			#FOR EACH LINE, CREATE AN ENTRY TO INSERT INTO THE DATABASE, AND TRIM
			#WHITESPACE FROM THE VALUE
			#datastream="$datastream\nINSERT INTO cl_ingests (value, LOG_TIME, is_offline, data_label) VALUES (trim(\"$thisvalue\"), str_to_date('$thisdate $thistime','%m-%d-%Y %T'), \"$is_disabled\", \"$i\");"
			echo ",$thisvalue,,$is_disabled,,$thisdate $thistime,$i,,">>$tmpfile;
			#echo "INSERT INTO cl_ingests (value, LOG_TIME, is_offline, data_label) VALUES (trim(\"$thisvalue\"), str_to_date('$thisdate $thistime','%m-%d-%Y %T'), \"$is_disabled\", \"$i\");">>$tmpfile;
			
		fi 
	done
}

#Accept the two digit hour, day, month, and four digit year to operate on.
if [ $# -lt 4 ]; then
		errors="You have fewer than four arguments."
		errors="$errors You must pass [2 digit hour] [2 digit day] [2 digit month] [4 digit year] seperated by spaces."
fi 
#SET UP A VARIABLE DETERMINING THE LENGTH OF THE ERRORS MESSAGE
error_length=`echo $errors | wc -c`
#create an array of active instruments
actives=`cat Config/config | grep active_inst= | sed 's/"//g' | sed 's/active_inst=/ /g'`
#obtain the saroad number
saroad=`cat Config/config | grep saroad= | sed 's/"//g' | sed 's/saroad=/ /g' | tr -d ' '`
#determine the name of the file to parse
file="$saroad-$4-$2-$3"
#TRANSLATE the active instruments array into an array of data value indicators
calused=''
indics=''
indicator=''
for i in $actives; do
	if (stat resources/$i/\.display &> /dev/null); then
		indicator=`(sed 's/\n//g')<resources/$i/\.display`
		#echo "this indicator is $indicator "
		if [ "$indics" == "" ]; then
			indics="$indicator"
		else
			indics="$indics $indicator"
		fi
	else
		#Modified to allow instruments add virtual cal channels
		if [ "$i" == "environics9100_drv" ] || [[ "$i" == *IZS* ]]; then
			calused='1'
		fi
		#WHAT SHOULD HAPPEN IF NO DISPLAY FILE IS PRESENT FOR THE CURRENT DRIVER
		sleep 2
	fi
done

if [ $error_length -gt 1 ]; then
		echo $errors
		exit
fi


#NOW WE NEED TO INGEST AN HOURS DATA FOR EACH OF THE INDICATORS
#THE INGEST SHOULD BE INDEPENDENT OF SAMPLING FREQUENCY
full_file="Data/$file"
tmpfile=.${$}prcssng 
#echo 'use carblogger;' > $tmpfile
############################################################
# ADDING MINUTE INDICATOR SO THAT THIS SCRIPT CAN BE RUN   #
# IN AN EFFORT TO REDUCE THE PROCESSING TIME FOR DATA 	   #
# INGEST, we'll add
# TO ONLY INGEST THE LAST 20 MINUTES OF THE HOUR SPECIFIED.#
############################################################
for i in $indics; do
	#create temporary process file
	#(grep $i | grep _${1}:${5} > .tmpprcsfile${$})<$full_file 
	(grep $i | grep _${1}:${5} | parsedata)<$full_file
	#rm .tmpprcsfile${$}
done 
#echo ";" >> $tmpfile
PASS=carblogger
#Show how many records needed ingesting
#echo "$counter lines to enter";
#Determine the number of records in the database prior to the operation
#BEFORE=`/usr/bin/mysql -u root --password=$PASS << EOF
#USE carblogger;
#SELECT COUNT(*) FROM cl_ingests;
#EOF
#`
#RESULT=`/usr/bin/mysql -u root --password=$PASS --force  < $tmpfile 2>/dev/null`
/usr/bin/mysql -u root --password="$PASS" carblogger <<EOF
	LOAD DATA LOCAL INFILE '/home/aqdms/CARBLog/$tmpfile' 
	INTO TABLE cl_ingests 
	FIELDS TERMINATED BY ',' 
	LINES TERMINATED BY '\n' 
	(@dummy, value, @dummy, is_offline, @dummy, @log_time, data_label, @dummy) 
	SET updated = NOW(), COMMENT = NULL, 
	LOG_TIME = str_to_date(@log_time,'%m-%d-%Y %T'), SAMP_TIME = NULL;
EOF
																
rm $tmpfile
#DEBUG-----
if [ "$5" = "DEBUG" ];then
	cat $tmpfile
	echo $calused
	echo $1
	echo $2
	echo $3
	echo $4
	echo $indics
	echo $saroad
	if [ -e "Data/$file" ]; then
	 	echo "$file found"
	else
		echo "$file not found"
	fi
fi
exit 
